package com.hundsun.cprs.yyt.service.system.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hundsun.cprs.yyt.service.system.domain.BussinessParam;
import com.hundsun.cprs.yyt.service.system.enums.EnumBussinessParam;

public class BusinessParamHelper {
    
    private static Log logger = LogFactory.getLog(BusinessParamHelper.class); 
    
    public static Integer getIntValue(BussinessParamService serivce,EnumBussinessParam param){
        return getIntValue(serivce, param, 0);
    }
    
    public static Integer getIntValue(BussinessParamService serivce,EnumBussinessParam param,Integer defaultValue){
        BussinessParam commissionRateParam = serivce.getBussinessParamByKey(param.getCode());
        if (commissionRateParam != null && commissionRateParam.getBussinessValue() != null) {
            try {
                return Integer.parseInt(commissionRateParam.getBussinessValue());
            } catch (NumberFormatException e) {
                 logger.error("系统[" + param.getValue() + "]配置有误,使用默认配置" + defaultValue+ "]");
            }
        }
        return defaultValue;
    }
    
    public static Double getDoubleValue(BussinessParamService serivce,EnumBussinessParam param){
        BussinessParam commissionRateParam = serivce.getBussinessParamByKey(param.getCode());
        if (commissionRateParam != null && commissionRateParam.getBussinessValue() != null) {
            try {
                return Double.parseDouble(commissionRateParam.getBussinessValue());
            } catch (NumberFormatException e) {
                 logger.error("系统[" + param.getValue() + "]配置有误,使用默认配置0]");
            }
        }
        return 0d;
    }

    public static String getStringValue(BussinessParamService serivce,EnumBussinessParam param,String defaultValue){
        BussinessParam bussinessParam = serivce.getBussinessParamByKey(param.getCode());
        if (bussinessParam != null && bussinessParam.getBussinessValue() != null) {
            try {
                return bussinessParam.getBussinessValue();
            } catch (NumberFormatException e) {
                logger.error("系统[" + param.getValue() + "]配置有误,使用默认配置" + defaultValue+ "]");
            }
        }
        return defaultValue;
    }

    public static Long getLongValue(BussinessParamService serivce,EnumBussinessParam param,Long defaultValue){
        BussinessParam commissionRateParam = serivce.getBussinessParamByKey(param.getCode());
        if (commissionRateParam != null && commissionRateParam.getBussinessValue() != null) {
            try {
                return Long.parseLong(commissionRateParam.getBussinessValue());
            } catch (NumberFormatException e) {
                logger.error("系统[" + param.getValue() + "]配置有误,使用默认配置" + defaultValue+ "]");
            }
        }
        return defaultValue;
    }
}
